//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <elatypes.h>

// The function ftol() is required by MSVC compiler to convert a float number
// to a long.  The implementation must comply with what the compiler expects.
// For more details, see (Intel) Pentium Processor Family Developer's Manual
// Volume 3: Architecture and Programming Manual (Charpter 6)
//
__declspec(naked) void __cdecl _ftol(void)
{
    __asm {
        push        ebp
        mov         ebp, esp
        sub         esp, 12

        fwait                           // check for pending numeric exceptions
        fnstcw      [ebp-2]             // write FPU control word to memory
        fwait

        mov         ax, [ebp-2]
        or          ah, 12
        mov         [ebp-4],ax

        fldcw       [ebp-4]             // replaces current FPU control word
        fistp       qword ptr [ebp-12]  // store integer
        fldcw       [ebp-2]

        mov         eax, [ebp-12]
        mov         edx, [ebp-8]

        leave
        ret
    }
}
__declspec(naked) void __cdecl _ftol2(void)
{
    __asm {
        push        ebp
        mov         ebp, esp
        sub         esp, 12

        fwait                           // check for pending numeric exceptions
        fnstcw      [ebp-2]             // write FPU control word to memory
        fwait

        mov         ax, [ebp-2]
        or          ah, 12
        mov         [ebp-4],ax

        fldcw       [ebp-4]             // replaces current FPU control word
        fistp       qword ptr [ebp-12]  // store integer
        fldcw       [ebp-2]

        mov         eax, [ebp-12]
        mov         edx, [ebp-8]

        leave
        ret
    }
}

__declspec(naked) void __cdecl _ftol2_sse(void)
{
    __asm {
        push        ebp
        mov         ebp, esp
        sub         esp, 12

        fwait                           // check for pending numeric exceptions
        fnstcw      [ebp-2]             // write FPU control word to memory
        fwait

        mov         ax, [ebp-2]
        or          ah, 12
        mov         [ebp-4],ax

        fldcw       [ebp-4]             // replaces current FPU control word
        fistp       qword ptr [ebp-12]  // store integer
        fldcw       [ebp-2]

        mov         eax, [ebp-12]
        mov         edx, [ebp-8]

        leave
        ret
    }
}
